﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using m.Channelvn.Cached.Common;
using EntityFramework;
using m.Channelvn.Cached.UrlRewrite;
using System.Text;
using BO;

namespace m.Website.Ajax
{
    public partial class AjaxListProduct : System.Web.UI.Page
    {
        protected const int PageSize = 9;
        protected bool noItem = true;
        private const string ProductFocusItemFormat = @"<div class=""item mb30 {11} {0}""  style=""width:97%"">
                    <div class=""img"">
                        <a href=""{4}"" title=""{3}""><img title=""{10}"" alt=""{3}"" src=""{5}"" /></a>
                    </div>
                    <div class=""content-right"">
                        {1}<div class=""name"">{2}</div>
                        <div class=""code-name"">{6}</div>
                        <div class=""desc"">{7}</div>
                        <div class=""shopping"">
                            <a href=""javascript:void(0);"" class=""cart""></a>
                            <div class=""price"">
                                <div class=""current"">{8}</div>
                                <div class=""discount"">{9}</div>
                            </div>
                        </div>
                    </div>    
                </div>";
        private const string ProductNewFormat = "<i class=\"new\"></i>";
        int iZoneid = 9999;
        protected int pageIndex, CompanyID, ZoneID, pageType;
        protected string catUrl;

        protected void Page_Load(object sender, EventArgs e)
        {

            catUrl = Lib.QueryString.CatUrl;
            pageIndex = Lib.QueryString.PageIndex;
            pageType = Lib.QueryString.PageType;
            CompanyID = Lib.QueryString.CompanyID;
            ZoneID = Lib.QueryString.ZoneID;

            var lst = ProductsBO.GetPagingProductByZoneProduct(pageIndex, PageSize, iZoneid, CompanyID);
            if (lst.Any())
            {
                noItem = false;
                var sb = new StringBuilder();
                var count = 1;
                foreach (var item in lst)
                {
                    var cls = count % 3 == 0 ? "mr0" : string.Empty;
                    long date = 0;
                    long.TryParse(item.EndDateNew, out date);
                    var pNew = date > 0 && date >= long.Parse(DateTime.Now.ToString("yyyyMMdd")) ? ProductNewFormat : string.Empty;

                    var price = item.TypeSale == 1 ? "Giá: Liên hệ" : Utils.ConvertMoneyNice(item.price.ToString(), BO.Const.FormatMoney);
                    var sale = item.TypeSale == 1 ? string.Empty : Utils.ConvertMoneyNice(item.sale.ToString(), BO.Const.FormatMoney);
                    var sapo = item.Sapo.ToString();
                    if (int.Parse(item.sale.ToString()) == 0)
                    {
                        sale = "";
                    }
                    sb.AppendFormat(ProductFocusItemFormat, cls
                        , pNew
                        , item.Title
                        , HttpUtility.HtmlEncode(item.Title)
                        , item.Url
                        , BO.Utils.CropImageMobile(172, 172, BO.Utils.GetOneImagesByArr(item.Avatar))
                        , item.ProductCode
                        , BO.Utils.getSubStringNice(item.Sapo, 50)
                        , price
                        , sale
                         , sapo
                                , count % 2 == 0 ? "even" : string.Empty);
                    count++;
                }
                if (lst.Count > 0)
                {
                    ltrListFocus.Text = sb.ToString();
                }
                else
                {
                    ltrListFocus.Text = "<h1>Hiện chưa có sản phẩm nào ở danh mục này</h1>";
                }
            }
        }
    }
}